package com.ynding.springboot.common.annotations;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * <p> </p>
 *
 * @author ynding
 * @version 2022/7/1
 **/
public class IntegerValidatorClass implements ConstraintValidator<IntegerValidator, Integer> {
    private IntegerValidator constraint;

    @Override
    public void initialize(IntegerValidator constraintAnnotation) {
        this.constraint = constraintAnnotation;
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if(value == null) {
            return false;
        }
        // 这个values就是你放在注解上的value数组，即 @FlagValidator(value = {0,1},message = "预约状态参数错误") 这里面的value值
        int[] values = constraint.value();
        // 遍历设定的值，当有一个值等于的话，就放行，否则拒绝放行
        for (int i : values) {
            if (i == value) {
                return true;
            }
        }
        return false;
    }
}
